import os


class OsUtil(object):
    def __init__(self, directory=None):
        self.init_dir_path = directory

    def __file_join_path__(self, root_path, file_name):
        return os.path.join(root_path, file_name)

    def exists_file(self, file_store_path):
        '''
        判断文件是否存在
        :param file_store_path: 具体文件路径
        :return: 返回True或False
        '''
        if file_store_path is None:
            return -1

        return 1 if os.path.exists(file_store_path) else 0

    def dir_scan(self, dir_path=None, prefix=None, postfix=None):
        '''
       返回扫描指定目录文件
       :param dir_path:  文件目录
       :param prefix: 文件名开始符合指定字符
       :param postfix: 指定字符符合指定文件名结尾
       :return: 返回扫描文件名集合
       '''

        if dir_path is None and self.init_dir_path is None:
            return None

        if dir_path is None:
            dir_path = self.init_dir_path

        for root, sub_dirs, files in os.walk(dir_path):
            for special_file in files:
                if postfix:
                    if special_file.endswith(postfix):
                        yield self.__file_join_path__(root, special_file)
                elif prefix:
                    if special_file.startswith(prefix):
                        yield self.__file_join_path__(root, special_file)
                else:
                    yield self.__file_join_path__(root, special_file)

#
# ost = OsUtil("d:\\fff\\")
# all_alist = ost.dir_scan(postfix=".txt")
# if all_alist:
#     for item in all_alist:
#         print(item)
